﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using HSDc.RingleFlow.Api.Kernel.Org;
using HSDc.RingleFlow.Kernel.DBPersit.DbOrg.Dao;

namespace HSDc.RingleFlow.Kernel.DBPersit.DbOrg
{
    internal class Department: IDept
    {
        private int _Id;
        private string _OrgId;
        private string _Name;
        private DepartmentDao dao = null;

        private List<IManager> _managers = new List<IManager>();
        private List<IEmp> _employees = new List<IEmp>();
        private Department _parent = null;
        private List<IDept> _children = new List<IDept>();

        internal Department(int _Id, string _OrgId, string _Name)
        {
            this._Id = _Id;
            this._OrgId = _OrgId;
            this._Name = _Name;
        }

        int IDept.Id
        {
            get { return _Id; }
        }
        
        string IDept.OrgId
        {
            get { return _OrgId; }
        }

        string IDept.Name
        {
            get { return _Name; }
        }

        IManager IDept.MainManager
        {
            get {
                RetrieveMainManager();
                return _managers[0];
            }
        }

        private void RetrieveMainManager()
        {
            InitialDao();
            _managers.Clear();
            EmployeeDto mngr = dao.RetrieveDepartmentMainManager(_Id);
            _managers.Add(new Manager(mngr.Emp_UserID, mngr.Emp_UserID, mngr.Emp_Name, mngr.Emp_EMail));
        }

        private void InitialDao()
        {
            if (dao == null)
                dao = new DepartmentDao();
        }

        List<IManager> IDept.Managers
        {
            get { 
                RetrieveManagers();
                return _managers;
            }
        }

        private void RetrieveManagers()
        {
            InitialDao();
            _managers.Clear();
            List<EmployeeDto> mngrs = dao.RetrieveDepartmentManagers(_Id);
            foreach(EmployeeDto mngr in mngrs)
                _managers.Add(new Manager(mngr.Emp_UserID, mngr.Emp_UserID, mngr.Emp_Name, mngr.Emp_EMail));
        }

        List<IEmp> IDept.Employee
        {
            get
            {
                RetrieveEmployees();
                return _employees;
            }
        }

        private void RetrieveEmployees()
        {
            InitialDao();
            _managers.Clear();
            List<EmployeeDto> emps = dao.RetrieveDepartmentEmployees(_Id);
            foreach (EmployeeDto emp in emps)
                _employees.Add(new Emp(emp.Emp_UserID, emp.Emp_UserID, emp.Emp_Name, emp.Emp_EMail));
        }

        IDept IDept.Parent
        {
            get {
                RetrieveParentDept();
                return _parent;
            }
        }

        private void RetrieveParentDept()
        {
            InitialDao();
            DepartmentDto parent = dao.RetrieveDeptParent(_Id);
            _parent = null;
            if (parent != null)
                _parent = new Department(parent.Dept_ID, parent.Dept_OrgID, parent.Dept_Name);
        }

        List<IDept> IDept.Children
        {
            get
            {
                RetrieveChildDept();
                return _children;
            }
        }

        private void RetrieveChildDept()
        {
            InitialDao();
            List<DepartmentDto> children = dao.RetrieveDeptChildren(_Id);
            _children.Clear();
            foreach(DepartmentDto child in children)
                _children.Add(new Department(child.Dept_ID, child.Dept_OrgID, child.Dept_Name));
        }
    }
}
